package com.hospital.drug.drugzhou.controller;

import com.alibaba.fastjson.JSON;
import com.hospital.drug.drugzhou.entity.DrugStock;
import com.hospital.drug.drugzhou.entity.Medicate;
import com.hospital.drug.drugzhou.service.MedicateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description：
 * @author：zou
 * @date：2021/9/16 13:28
 * @versions：v1.0
 */
@Controller
@RequestMapping("/medicate")
public class MedicateController {

    @Autowired
    private MedicateService medicateService;

    //申请记录分页
    @RequestMapping("/applyList")
    @ResponseBody
    public Object applyList(Integer page, Integer limit, Integer date, Integer type) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 5;
        }
        return medicateService.selPage(date, type, limit, page);
    }

    //增加库存申请
    @RequestMapping("/inventoryApply")
    @ResponseBody
    public String inventoryApply(HttpServletRequest request) {

        String name = request.getParameter("name");      //申请人
        String date = request.getParameter("date");      //申请时间

        //将字符串转换成时间
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date parse = null;  //时间
        try {
            parse = format1.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //将时间转换成指定的字符串
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format2.format(parse);

        String drugs = request.getParameter("list");    //药品集合 json集合

        List<DrugStock> list = JSON.parseArray(drugs, DrugStock.class);     //将json对象数组转换成java集合对象

        int count = medicateService.drugInventory(list, dateStr);   //添加药品库存

        String type = request.getParameter("type");        //类型 【补充库存】
        String status = request.getParameter("status");    //申请状态 【0：未通过】
        String remark = request.getParameter("remark");    //备注

        Medicate medicate = new Medicate(null, name, dateStr, count, null, null, null, null, null, type, Integer.parseInt(status), remark);  //申请记录

        boolean b = medicateService.addInventory(medicate);    //添加库存申请

        return JSON.toJSONString(b);
    }

    //增加新药申请
    @RequestMapping("/newDrug")
    @ResponseBody
    public String newDrug(HttpServletRequest request) {

        String name = request.getParameter("name");      //申请人
        String date = request.getParameter("date");      //申请时间

        //将字符串转换成时间
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date parse = null;  //时间
        try {
            parse = format1.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format2.format(parse);

        String drug = request.getParameter("drug");            //药品名称
        String drugType = request.getParameter("drugType");    //药品类型
        String unit = request.getParameter("unit");            //药品单位 例:[瓶、盒]
        String price = request.getParameter("price");          //药品价格
        String count = request.getParameter("count");          //数量
        String type = request.getParameter("type");            //类型 【新增药品】
        String status = request.getParameter("status");        //申请状态 【0：未通过】
        String remark = request.getParameter("remark");        //备注

        Medicate medicate = new Medicate(null, name, dateStr, null, drug, Integer.parseInt(drugType), unit, Double.parseDouble(price), Integer.parseInt(count), type, Integer.parseInt(status), remark);  //申请记录

        boolean b = medicateService.addDrug(medicate);    //添加新药申请

        return JSON.toJSONString(b);
    }

    //药品管理
    @RequestMapping("/drugManage")
    @ResponseBody
    public String drugManage(HttpServletRequest request) {

        String id = request.getParameter("id");           //编号
        String name = request.getParameter("name");       //申请人
        String date = request.getParameter("date");      //申请时间
        String drugName = request.getParameter("drugName");  //药品名称
        String drugType = request.getParameter("drugType");  //药品类型
        String unit = request.getParameter("unit");          //药品单位 例:[瓶、盒]
        String price = request.getParameter("price");        //药品价格
        String count = request.getParameter("count");        //药品数量
        String type = request.getParameter("type");        //申请类型
        String remark = request.getParameter("remark");      //备注

        if (drugType == null || drugType.equals("")) {
            drugType = "0";
        }
        if (price == null || price.equals("")) {
            price = "0";
        }
        if (count == null || count.equals("")) {
            count = "0";
        }

        Medicate medicate = new Medicate(Integer.parseInt(id), name, date, 0, drugName, Integer.parseInt(drugType), unit, Double.parseDouble(price), Integer.parseInt(count), type, 0, remark);

        int status = medicateService.drugManage(medicate);

        return JSON.toJSONString(status);
    }

    //根据时间查询同一批药品
    @RequestMapping("/drugStock")
    @ResponseBody
    public String drugStock(String date) {
        List<DrugStock> drugList = medicateService.drugList(date);
        return JSON.toJSONString(drugList);
    }

}
